#include "PrecompiledHeader.h"
#include "CMixer.h"

/*--------------------------------------------------------------------------*/
CMixer* g_Mixer = NULL;
/*--------------------------------------------------------------------------*/

CMixer::CMixer() :
	IEngMod("CMixer"),
	ICriticalSection(1),
	IRender(ERenderFlag::None)
{
	AssertModule(g_Shell);
	g_Shell->RegisterLuaFunction("playmusic", sh::playmusic);
	g_Shell->RegisterLuaFunction("playchunk", sh::playchunk);

	this->m_soundtrackNumber = 0;
	this->m_playingSoundtrack = false;

	AssertModule(g_Sdl);
	Mix_HookMusicFinished(OnMusicStopHook);

// 	this->AddRenderTask(g_Material->GetNullMaterial(),
// 	                    0,
// 	                    vl::Render::UserInterface,
// 	                    RenderTask::None,
// 	                    0);
	this->StartTask(0, CalcDesc(true, ECalc::MultiThreadHighPriority));
}

/*--------------------------------------------------------------------------*/

CMixer::~CMixer()
{

}

/*--------------------------------------------------------------------------*/

void CMixer::PlayNow(const Sound* sound)
{
	if (sound != NULL)
	{
		Mix_PlayChannel(-1, sound->GetChunk(), 0);
	}
}

/*--------------------------------------------------------------------------*/

void CMixer::PlayNow(const Music* music)
{
	if (music != NULL)
	{
		g_Shell->Execute("print('started new mp3')");

		if (Mix_PlayingMusic())
		{
			Mix_HaltMusic();
		}

		Mix_PlayMusic(music->GetMusic(), 1);
	}
}

/*--------------------------------------------------------------------------*/

void CMixer::OnRender(const RenderTask* renderTask, uint camarg)
{
	switch (renderTask->GetArgument())
	{
	case 0:
	{
		//OpenGL::DrawRectangle(glm::vec2(), glm::vec2(g_Settings->m_resolutionf.x, 15.0f), vl::Black);
		//g_Hud->DrawText(this->m_displayedLabel, glm::vec2(), vl::White);
		break;
	}
	}
}

/*--------------------------------------------------------------------------*/

void CMixer::OnCalc(uint arg)
{
	//this->BindRenderTasks();
}

/*--------------------------------------------------------------------------*/

void CMixer::AddToSoundtrack(const Music* music)
{
	this->Lock(Soundtrack);
	this->m_soundtrack.push_back(music);
	this->Unlock(Soundtrack);
}

/*--------------------------------------------------------------------------*/

void CMixer::AddToSoundtrack(const std::string& name)
{
	Music* m = NULL;
	g_Resources->Get(name, &m);

	if (m != NULL)
	{
		this->AddToSoundtrack(m);
	}
}

/*--------------------------------------------------------------------------*/

void CMixer::ClearSoundtrack()
{
	this->Lock(Soundtrack);
	this->m_soundtrack.clear();
	this->Unlock(Soundtrack);
}

/*--------------------------------------------------------------------------*/

void CMixer::OnMusicStop()
{
	if (this->m_playingSoundtrack)
	{
		++this->m_soundtrackNumber;

		if (this->m_soundtrackNumber < this->m_soundtrack.size())
		{
			this->PlayNow(this->m_soundtrack[this->m_soundtrackNumber]);
		}
		else
		{
			this->m_soundtrackNumber = 0;
			this->m_playingSoundtrack = false;
		}
	}
}

/*--------------------------------------------------------------------------*/

void CMixer::StartPlayingSoundtrack()
{
	this->m_playingSoundtrack = true;
	this->m_soundtrackNumber = 0;

	if (this->m_soundtrackNumber < this->m_soundtrack.size())
	{
		this->PlayNow(this->m_soundtrack[this->m_soundtrackNumber]);
	}
}

/*--------------------------------------------------------------------------*/

void CMixer::StopMusicFade(int ms)
{
	Mix_FadeOutMusic(ms);
}

/*--------------------------------------------------------------------------*/

void CMixer::StepMusic()
{
	Mix_HaltMusic();
}

/*--------------------------------------------------------------------------*/

void OnMusicStopHook()
{
	AssertModule(g_Mixer);

	g_Mixer->OnMusicStop();
}

/*--------------------------------------------------------------------------*/

void sh::playmusic(const std::string& name)
{
	AssertModule(g_Resources);
	AssertModule(g_Mixer);

	Music* music = NULL;
	g_Resources->Get(name, &music);
	g_Mixer->PlayNow(music);
}

/*--------------------------------------------------------------------------*/

void sh::playchunk(const std::string& name)
{
	AssertModule(g_Resources);
	AssertModule(g_Mixer);

	Sound* sound = NULL;
	g_Resources->Get(name, &sound);
	g_Mixer->PlayNow(sound);
}

/*--------------------------------------------------------------------------*/
